package lock;

import java.util.concurrent.locks.ReentrantLock;

/*
可冲入锁(Lock)演示
 */
public class ThreadDemo8 {

  public static void main(String[] args) {

    ReentrantLock lock = new ReentrantLock();

    new Thread(()->{

      try{
//        上锁
        lock.lock();
        System.out.println(Thread.currentThread().getName() + "外层");

        try{
//        上锁
          lock.lock();
          System.out.println(Thread.currentThread().getName() + "内层");
        }finally {
//        解锁
//          lock.unlock();
        }
      }finally {
//        解锁
        lock.unlock();
      }


    },"t1").start();

    new Thread(()->{

      try{
        lock.lock();
        System.out.println(Thread.currentThread().getName() + "最外层");
      }finally {
        lock.unlock();
      }
    },"t2").start();

  }
}
